package com.itshare.video.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itshare.video.common.core.constant.enums.ApiInfo;
import com.itshare.video.common.core.constant.enums.DeleteStatus;
import com.itshare.video.common.dto.PageEntity;
import com.itshare.video.common.dto.QueryInfo;
import com.itshare.video.common.dto.Result;
import com.itshare.video.mapper.UserMessageMapper;
import com.itshare.video.mapper.VideoMapper;
import com.itshare.video.mapper.custom.UserMapperCustom;
import com.itshare.video.mapper.custom.UserMessageMapperCustom;
import com.itshare.video.pojo.bo.FeedBackMessageBO;
import com.itshare.video.pojo.bo.ReportVideoMessageBO;
import com.itshare.video.pojo.code.MessageType;
import com.itshare.video.pojo.dto.ReplyMsgDTO;
import com.itshare.video.pojo.dto.UserTokenDTO;
import com.itshare.video.pojo.po.User;
import com.itshare.video.pojo.po.UserMessage;
import com.itshare.video.pojo.query.UserMessageQuery;
import com.itshare.video.service.UserMessageService;
import com.itshare.video.util.MessageUtil;
import com.itshare.video.util.mq.UserMessageMq;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author lyr
 * @create 2020/10/31 14:30
 */
@Service
public class UserMessageServiceImpl implements UserMessageService {
    @Resource
    private UserMessageMapperCustom userMessageMapperCustom;
    @Resource
    private UserMessageMapper userMessageMapper;
    @Resource
    private UserMessageMq userMessageMq;
    @Resource
    private VideoMapper videoMapper;

    /**
     * 获取个人消息推送
     *
     * @param userId
     * @return
     */
    @Override
    public PageEntity<UserMessage> getUserMessage(Integer userId, Integer messageType) {
        UserMessageQuery userMessageQuery = UserMessageQuery
                .builder()
                .messageType(messageType)
                .toUserId(userId)
                // .hasRead(0)
                .build();

        // PageEntity.of();
        return PageEntity.of(
                userMessageMapperCustom.getUserMessage(userMessageQuery)
        );
    }


    /**
     * 获取用户举报 或者 反馈的消息
     *
     * @param userTokenDTO
     * @return
     */
    @Override
    public PageEntity<UserMessage> getFeedBackMessage(UserTokenDTO userTokenDTO) {
        List<UserMessage> result = userMessageMapperCustom.getFeedBackMessage(userTokenDTO.getId());
        return PageEntity.of(result);
    }


    @Resource
    private UserMapperCustom userMapperCustom;

    /**
     * 举报视频内容
     *
     * @param videoMessageBO
     * @param userTokenDTO
     * @return
     */
    @Override
    public Result<?> reportVideoIllegal(ReportVideoMessageBO videoMessageBO, UserTokenDTO userTokenDTO) {
        User user = userMapperCustom.selectUserByPrimaryKeys(userTokenDTO.getId().longValue(), null, null);
        UserMessage userMessage = new UserMessage()
                .withHasRead(0)
                .withMessageType(MessageType.REPORT_MSG.type)
                .withCreateTime(new Date())
                .withUpdateTime(new Date())
                .withToUserMessage(videoMessageBO.getMessage())
                .withVideoid(videoMessageBO.getVideoId())
                .withFromUserId(userTokenDTO.getId())
                .withFromUserName(user.getUserName())
                .withDeleteStatus(0)
                .withFeedbackTitle(videoMessageBO.getFeedbackTitle());
        userMessageMq.sentMsg(userMessage);
        return Result.of(ApiInfo.OK);
    }

    private UserMessage getDefaultUserMsg() {
        UserMessage userMessage = new UserMessage()
                .withHasRead(0)
                .withCreateTime(new Date())
                .withDeleteStatus(0)
                .withUpdateTime(new Date());
        return userMessage;
    }


    /**
     * 用户反馈
     *
     * @param feedBackMessageBO
     * @param userTokenDTO
     * @return
     */
    @Override
    public Result<?> userFeedback(FeedBackMessageBO feedBackMessageBO, UserTokenDTO userTokenDTO) {
        User user = userMapperCustom.selectUserByPrimaryKeys(userTokenDTO.getId().longValue(), null, null);
        UserMessage userMessage = getDefaultUserMsg();
        // 发送用户反馈消息 ,feedback
        userMessage
                .withImage(feedBackMessageBO.getImage())
                .withFromUserPhone(feedBackMessageBO.getPhone())
                .withFeedbackTitle(feedBackMessageBO.getFeedbackTitle())
                .withToUserMessage(feedBackMessageBO.getFeedbackContent())
                .withFromUserName(user.getUserName())
                .withFromUserId(userTokenDTO.getId())
                .withMessageType(MessageType.FEEDBACK_MSG.type);
        userMessageMq.sentMsg(userMessage);
        return Result.of(ApiInfo.OK);
    }

    /**
     * 处理 举报不通过
     *
     * @param messageId
     * @return
     */
    @Override
    public Result<?> handleFeedbackNoPass(Integer messageId) {
        UserMessage userMessage = userMessageMapper.selectByPrimaryKey(messageId);
        userMessage.setHasRead(1);
        userMessageMapper.updateByPrimaryKeySelective(userMessage);
        //发送新消息给 用户,告诉他已读
        UserMessage newMessage = MessageUtil.getDefaultMsg()
                .withToUserId(userMessage.getFromUserId())
                .withToUserName(userMessage.getFromUserName())
                .withToUserMessage("您的视频举报不通过")
                .withFeedbackTitle("举报不通过")
                .withMessageType(MessageType.SYS_MSG.type);
        userMessageMq.sentMsg(newMessage);
        //您的举报不通过
        return Result.of(ApiInfo.OK);
    }

    /**
     * 用户举报成功
     *
     * @param messageId
     */
    @Override
    public void sendFeedbackMessageOK(Integer messageId) {
        UserMessage userMessage = userMessageMapper.selectByPrimaryKey(messageId);
        UserMessage newMessage = MessageUtil.getDefaultMsg();
        //推送举报成功通知
        newMessage.withToUserMessage("视频举报成功,已经将违规视频下架")
                .withFeedbackTitle("视频举报成功")
                .withFromUserName("sys")
                .withMessageType(MessageType.SYS_MSG.type)
                .withToUserId(userMessage.getFromUserId())
                .withToUserName(userMessage.getFromUserName());
        userMessageMq.sentMsg(newMessage);
    }

    /**
     * 删除消息
     *
     * @param msgId
     */
    @Override
    public void deleteMsg(Integer msgId) {
        UserMessage userMessage = new UserMessage()
                .withMessageId(msgId)
                .withHasRead(1)
                .withDeleteStatus(DeleteStatus.deleted.type);
        userMessageMapper.updateByPrimaryKeySelective(userMessage);
    }

    @Override
    public void replyMsg(Integer msgId, ReplyMsgDTO replyMsgDTO, UserTokenDTO userTokenDTO) {
        UserMessage userMsg = userMessageMapper.selectByPrimaryKey(msgId);
        UserMessage msg = MessageUtil
                .getDefaultMsg()
                .withFeedbackTitle("管理员回复")
                .withMessageType(MessageType.SYS_MSG.type)
                .withToUserId(userMsg.getFromUserId())
                .withToUserName(userMsg.getFromUserName())
                .withToUserMessage(replyMsgDTO.getReplyContent())
                .withFromUserId(userTokenDTO.getId());
        //插入回复数据
        userMessageMapper.insertSelective(msg);
    }


    @Override
    public PageEntity<UserMessage> getFeedbackMsg(Integer msgType, QueryInfo queryInfo) {
        PageInfo<UserMessage> pageInfo = PageHelper.startPage(queryInfo.getPageParam())
                .doSelectPageInfo(() -> {
                    userMessageMapperCustom.getFeedbackMsg(msgType);

                });
        return PageEntity.of(pageInfo);
    }
}
